package org.tf;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.SoftApConfiguration;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;

public class WifiActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener, CompoundButton.OnCheckedChangeListener {
    private final static String TAG = "Wifi";
    private static final int MIN_CH_2G_BAND = 1;
    private static final int MAX_CH_2G_BAND = 14;
    private static final int MIN_CH_5G_BAND = 34;
    private static final int MAX_CH_5G_BAND = 196;
    private static final int BAND_2GHZ = 1 << 0;
    private static final int BAND_5GHZ = 1 << 1;
    private static final String[]  band_name= {"2.4G", "5G"};
    int ids[] = {R.id.startap, R.id.stopap};
    private WifiManager mWifiManger;
    int bandMode = BAND_2GHZ;
    boolean usePassword = false;
    EditText txtPassword;
    EditText txtApname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi);
        mWifiManger = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        initUI();
    }
    private void initUI(){
        for(int id: ids){
            findViewById(id).setOnClickListener(this);
        }
        ((RadioGroup) findViewById(R.id.mode_choose)).setOnCheckedChangeListener(this);
        CheckBox box = findViewById(R.id.password);
        box.setOnCheckedChangeListener(this);
        txtPassword = findViewById(R.id.txtPassword);
        txtApname = findViewById(R.id.txtApname);
    }

    private void doStartAp(){
        Log.i(TAG, "doStartAp");
        String apname = txtApname.getText().toString();
        if(usePassword){
            mWifiManger.startTetheredHotspot(getWpa2SoftAp(apname, txtPassword.getText().toString()));
        }else{
            mWifiManger.startTetheredHotspot(getOpenSoftAp(apname));
        }
    }
    private SoftApConfiguration getOpenSoftAp(String ssid){
        int ch = (bandMode == BAND_2GHZ) ? 6 : 36;
        SoftApConfiguration.Builder builder = new SoftApConfiguration.Builder();
        Log.i(TAG, "ssid=" + ssid + ", band=" + band_name[bandMode>>1] + ", ch=" + ch);
        builder.setSsid(ssid)
                .setChannel(ch, bandMode);
        return builder.build();
    }

    private SoftApConfiguration getWpa2SoftAp(String ssid, String password){
        int ch = (bandMode == BAND_2GHZ) ? 6 : 36;
        SoftApConfiguration.Builder builder = new SoftApConfiguration.Builder();
        builder.setPassphrase(password, SoftApConfiguration.SECURITY_TYPE_WPA2_PSK)
                .setSsid(ssid)
                .setChannel(ch, bandMode);
        Log.i(TAG, "ssid=" + ssid + ", password=" + password + ", band=" + band_name[bandMode>>1] + ", ch=" + ch);
        return builder.build();
    }

    private void doStopAp(){
        boolean status = mWifiManger.stopSoftAp();
        Log.i(TAG, "doStopAp, status=" + status);
    }

    @Override
    public void onClick(View v) {
        Log.i(TAG,"onClick");
        switch (v.getId()){
            case R.id.startap:
                doStartAp();
                break;
            case R.id.stopap:
                doStopAp();
                break;
            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int id = group.getCheckedRadioButtonId();
        switch (id){
            case R.id.mode2g:
                bandMode = BAND_2GHZ;
                break;
            case R.id.mode5g:
                bandMode = BAND_5GHZ;
                break;
        }
        Log.i(TAG,"onCheckedChanged, bnadMode=" +  bandMode);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        usePassword = isChecked ? true : false;
        Log.i(TAG,"onCheckedChanged, usePassword=" +  usePassword);
    }
}